package com.gs.common.tool.utils;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.Time;
import java.util.Date;
import java.util.Objects;

/**
 * 数据库类型转java类型工具类
 *
 * @author: guoshuai
 * @date: 2024/05/27 12:31
 **/
public class DbTypeToJavaTypeUtil {


    /**
     * 数据库类型转java类型
     *
     * @param columnType 列类型
     */
    public static Type dbTypeToJavaType(String columnType) {
        if (StringUtil.isEmpty(columnType)) {
            throw new IllegalArgumentException("table columnType is not null");
        }
        columnType = columnType.trim().toLowerCase();
        Type javaType = null;
        switch (columnType) {
            case "string":
            case "text":
            case "varchar":
                javaType = String.class;
                break;
            case "tinyint":
            case "int":
            case "integer":
                javaType = Integer.class;
                break;
            case "bigint":
            case "long":
                javaType = Long.class;
                break;
            case "float":
                javaType = Float.class;
                break;
            case "double":
            case "decimal":
                javaType = Double.class;
                break;
            case "date":
            case "datetime":
            case "timestamp":
                javaType = Date.class;
                break;
            case "boolean":
            case "bit":
                javaType = Boolean.class;
                break;
            case "time":
                javaType = Time.class;
                break;
        }
        if (columnType.startsWith("timestamp")) {
            javaType = Date.class;
        }
        if (columnType.startsWith("numeric")) {
            javaType = BigDecimal.class;
        }
        if (columnType.startsWith("double")) {
            javaType = Double.class;
        }
        return javaType;
    }


    /**
     * 数据库类型转java类型
     *
     * @param columnType 列类型
     */
    public static String dbTypeToJavaTypeName(String columnType) {
        if (StringUtil.isEmpty(columnType)) {
            throw new IllegalArgumentException("table columnType is not null");
        }
        columnType = columnType.trim().toLowerCase();
        Class<?> javaType = null;
        switch (columnType) {
            case "string":
            case "text":
            case "varchar":
                javaType = String.class;
                break;
            case "tinyint":
            case "int":
            case "integer":
                javaType = Integer.class;
                break;
            case "bigint":
            case "long":
                javaType = Long.class;
                break;
            case "float":
                javaType = Float.class;
                break;
            case "double":
            case "decimal":
                javaType = Double.class;
                break;
            case "date":
            case "datetime":
            case "timestamp":
                javaType = Date.class;
                break;
            case "boolean":
            case "bit":
                javaType = Boolean.class;
                break;
            case "time":
                javaType = Time.class;
                break;
        }
        if (columnType.startsWith("timestamp")) {
            javaType = Date.class;
        }
        if (columnType.startsWith("numeric")) {
            javaType = BigDecimal.class;
        }
        if (columnType.startsWith("double")) {
            javaType = Double.class;
        }
        return Objects.isNull(javaType) ? null : javaType.getSimpleName();
    }
}
